home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / QuickTime / Simple Imagecompressor / GetQTCompressedPict.c < prev    next >
Encoding:
Text File  |  1994-10-28  |  2.8 KB  |  104 lines  |  [TEXT/KAHL]

  1. // this is a routine to return a compressed pict from a pixmap
  2. //
  3. // Nick Thompson, June '94
  4. //
  5. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  6.  
  7.  
  8. #include <Types.h>
  9. #include <Traps.h>
  10. #include <Memory.h>
  11. #include <Errors.h>
  12. #include <FixMath.h>
  13. #include "Movies.h"
  14. #include "ImageCompression.h"
  15. #include "QuickTimeComponents.h"
  16.  
  17. #include "GetQTCompressedPict.h"
  18.  
  19. //---------------------------------------------------------------------------------------
  20. // return a compressed pict from a pixmap
  21.  
  22. PicHandle GetQTCompressedPict( PixMapHandle myPixMap, SCParams *params )
  23. {
  24.     long                            maxCompressedSize = 0;
  25.     Handle                            compressedDataH = nil;
  26.     Ptr                                compressedDataP;
  27.     ImageDescriptionHandle            imageDescH = nil;
  28.     OSErr                            theErr;
  29.     PicHandle                        myPic = nil;
  30.     Rect                            bounds = (**myPixMap).bounds;
  31.     CodecType                        theCodecType = 'jpeg';
  32.     CodecComponent                    theCodec = (CodecComponent)anyCodec;
  33.     CodecQ                            spatialQuality = codecNormalQuality;
  34.     short                            depth = 0;        /* let ICM choose depth */
  35.  
  36.     theErr = GetMaxCompressionSize(    myPixMap, 
  37.                                     &bounds,
  38.                                     params->depth,                    // pixel depth to compress image at 
  39.                                     params->spatialQuality,            // spatial quality to compress image at 
  40.                                     params->theCodecType,            // type of codec to use 'jpeg','rpza', etc. 
  41.                                     params->theCodec,                // codec to use either in general 
  42.                                                                     // terms (bestSpeed, bestFidelity,etc.) 
  43.                                     &maxCompressedSize);
  44.  
  45.     CheckError( theErr, "\pError in GetMaxCompressionSize") ;
  46.     
  47.     if ( theErr ) return nil;
  48.     
  49.     imageDescH = (ImageDescriptionHandle)NewHandle(4);
  50.     
  51.     compressedDataH = NewHandle(maxCompressedSize);
  52.     
  53.     if ( compressedDataH != nil && imageDescH != nil  ) 
  54.     { 
  55.         MoveHHi(compressedDataH);
  56.         HLock(compressedDataH);
  57.         compressedDataP = StripAddress(*compressedDataH);
  58.     
  59.         theErr = CompressImage( myPixMap,
  60.                         &bounds,
  61.                         params->spatialQuality,
  62.                         params->theCodecType,
  63.                         imageDescH,
  64.                         compressedDataP    );
  65.                         
  66.                         
  67.         CheckError( theErr, "\pError in CompressImage") ;
  68.  
  69.         
  70.         if ( theErr == noErr ) 
  71.         {                        
  72.             OpenCPicParams            myOpenCPicparams ;
  73.             
  74.             myOpenCPicparams.srcRect = bounds ;
  75.             myOpenCPicparams.hRes = (**imageDescH).hRes ;
  76.             myOpenCPicparams.vRes = (**imageDescH).vRes ;
  77.             myOpenCPicparams.version = -2 ;
  78.     
  79.             ClipRect(&bounds);
  80.             
  81.             myPic = OpenCPicture(&myOpenCPicparams);
  82.             theErr = DecompressImage( compressedDataP,
  83.                                         imageDescH,
  84.                                         myPixMap,
  85.                                         &bounds,
  86.                                         &bounds,
  87.                                         srcCopy,
  88.                                         nil );
  89.                                         
  90.             CheckError( theErr, "\pError in DecompressImage") ;
  91.             
  92.             ClosePicture();
  93.         }
  94.         if ( theErr != noErr || GetHandleSize((Handle)myPic) == sizeof(Picture) ) 
  95.         {
  96.             KillPicture(myPic);
  97.             myPic = nil;
  98.         }
  99.     }
  100.     if (imageDescH) DisposeHandle( (Handle)imageDescH);
  101.     if (compressedDataH) DisposeHandle( compressedDataH);
  102.  
  103.     return myPic;
  104. }